package posecco.server;

import posecco.client.RegistrationService;
import posecco.client.Role;
import posecco.client.User;

import com.google.appengine.api.datastore.QueryResultIterator;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RegistrationServiceImpl extends RemoteServiceServlet implements
		RegistrationService {

	public User addUser(String login, String pwd, String pwdConfirm, String email,
			String firstName, String lastName) throws IllegalArgumentException {
		
		if (!loginTaken(null, login)) {
			try {
				ObjectifyService.register(User.class);
			}
			catch (IllegalArgumentException e) {}
	        Objectify ofy = ObjectifyService.begin();
	
	        User user = new User(login, pwd, email, firstName, lastName, null);
	        //Use setters to populate the object
	        //the Id will be auto generated and does not need to be set
	        ofy.put(user);
	        assert user.getId() != null;    // id was autogenerated
	        user = ofy.query(User.class).filter("login", login).get();
	 		return user;
		}
		else {
			
			return null;
		}
 	}

	public User updateUser(Long id, String login, String email,
			String firstName, String lastName) throws IllegalArgumentException {
		
		try {
			ObjectifyService.register(User.class);
		}
		catch (IllegalArgumentException e) {}
        Objectify ofy = ObjectifyService.begin();

        User user = ofy.query(User.class).filter("id", id).get();
        user.setLogin(login);
        user.setEmail(email);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        
        ofy.put(user);
        assert user.getId() != null;
        user = ofy.query(User.class).filter("login", login).get();
 		return user;
 	}

	public Boolean loginTaken(Long id, String login) {
		try {
			ObjectifyService.register(User.class);
		}
		catch (IllegalArgumentException e) {}
        Objectify ofy = ObjectifyService.begin();
        Query<User> query = ofy.query(User.class).filter("loginLower", login.toLowerCase());
        if (id != null) {
        	
        	query = query.filter("id <>", id);
        }
        User user = query.get();
        try {
        	
            if (user != null && (user.getId() > 0)) {
            	
            	return true;
            }
            return false;
        }
        catch (NullPointerException e) {
        	
        	return false;
        }
	}
	
	public Boolean createRoleList(Panel parent) {
		
		try {
			ObjectifyService.register(Role.class);
		}
		catch (IllegalArgumentException e) {}
        Objectify ofy = ObjectifyService.begin();
        Query<Role> roles = ofy.query(Role.class);
        QueryResultIterator<Role> iterator = roles.iterator();
        ListBox list = new ListBox();
        while (iterator.hasNext()) {
            Role role = iterator.next();
            list.addItem(role.getName());
        }
        parent.add(list);
        return true;
	}
}
